import baseUrl from '../config/env'
export default {
  data() {
    return {
      baseUrl: baseUrl
    }
  },
  methods: {
    outerHeight(el) {
      var height = el.offsetHeight
      var style = window.getComputedStyle(el)

      height += parseInt(style.marginTop) + parseInt(style.marginBottom)
      return height
    },
    getElement(el) {
      if (el === undefined) return
      if (el[0] === '.') {
        return document.getElementsByClassName(el.substring(1))
      } else if (el[0] === '#') {
        return document.getElementById(el.substring(1))
      } else {
        return document.getElementsByTagName(el)
      }
    },
    //节流函数 取至underscore https://github.com/jashkenas/underscore/blob/master/underscore.js
    throttle (func, wait, options) {
      var timeout, context, args, result;
      var previous = 0;
      if (!options) options = {};

      var later = function () {
        previous = options.leading === false ? 0 : new Date();
        timeout = null;
        result = func.apply(context, args);
        if (!timeout) context = args = null;
      };

      var throttled = function () {
        var now = new Date();
        if (!previous && options.leading === false) previous = now;
        var remaining = wait - (now - previous);
        context = this;
        args = arguments;
        if (remaining <= 0 || remaining > wait) {
          if (timeout) {
            clearTimeout(timeout);
            timeout = null;
          }
          previous = now;
          result = func.apply(context, args);
          if (!timeout) context = args = null;
        } else if (!timeout && options.trailing !== false) {
          timeout = setTimeout(later, remaining);
        }
        return result;
      };

      throttled.cancel = function () {
        clearTimeout(timeout);
        previous = 0;
        timeout = context = args = null;
      };

      return throttled;
    }
  }
}
